package com.kingmed.kmss.admin.modules.ums.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingmed.kmss.admin.modules.ums.model.UmsResource;
import com.kingmed.kmss.admin.modules.ums.service.UmsAdminCacheService;
import com.kingmed.kmss.admin.modules.ums.service.UmsResourceService;
import com.kingmed.kmss.common.vo.CommonPage;
import com.kingmed.kmss.common.vo.CommonResponse;
import com.kingmed.kmss.common.vo.CommonResponseHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台资源管理Controller
 */
@Controller
@Api(tags = "ums资源管理")
@RequestMapping("/ums/resource")
public class UmsResourceController {

    @Autowired
    private UmsResourceService resourceService;
    @Autowired
    private UmsAdminCacheService adminCacheService;

    @ApiOperation("添加后台资源")
    @PostMapping(value = "/create")
    @ResponseBody
    public CommonResponse<String> create(@Validated @RequestBody UmsResource umsResource) {
        boolean success = resourceService.create(umsResource);
        if (success) {
            return CommonResponseHelper.success(null);
        } else {
            return CommonResponseHelper.fail();
        }
    }

    @ApiOperation("修改后台资源")
    @PostMapping(value = "/update")
    @ResponseBody
    public CommonResponse<String> update(@Validated @RequestBody List<UmsResource> umsResourceList) {
        boolean success = resourceService.saveOrUpdateBatch(umsResourceList);
        for (UmsResource umsResource : umsResourceList) {
            if (umsResource.getId() != null) {
                adminCacheService.delResourceListByResource(umsResource.getId());
            }
        }
        if (success) {
            return CommonResponseHelper.success(null);
        } else {
            return CommonResponseHelper.fail();
        }
    }

    @ApiOperation("根据ID获取资源详情")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public CommonResponse<UmsResource> getItem(@PathVariable Long id) {
        UmsResource umsResource = resourceService.getById(id);
        return CommonResponseHelper.success(umsResource);
    }

    @ApiOperation("根据ID删除后台资源")
    @PostMapping(value = "/delete/{id}")
    @ResponseBody
    public CommonResponse<String> delete(@PathVariable Long id) {
        boolean success = resourceService.delete(id);
        if (success) {
            return CommonResponseHelper.success(null);
        } else {
            return CommonResponseHelper.fail();
        }
    }

    @ApiOperation("分页模糊查询后台资源")
    @GetMapping(value = "/list")
    @ResponseBody
    public CommonResponse<CommonPage<UmsResource>> list(@RequestParam(required = false) Long menuID,
                                                        @RequestParam(required = false) String nameKeyword,
                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<UmsResource> resourceList = resourceService.list(menuID, nameKeyword, pageSize, pageNum);
        return CommonResponseHelper.success(CommonPage.restPage(resourceList));
    }

    @ApiOperation("查询所有后台资源")
    @GetMapping(value = "/listAll")
    @ResponseBody
    public CommonResponse<List<UmsResource>> listAll() {
        List<UmsResource> resourceList = resourceService.list();
        return CommonResponseHelper.success(resourceList);
    }
}
